import request from '@/Intercepter/request'
import axios from 'axios'
import { Result } from '@/types'
import type { Hotel } from '@/types'

export function apiLogin(data: { username: string, password: string }) {
  return axios.post<Result<string>>('http://localhost:8080/user/login', data)   // 返回类型为Result<string>
}

export function apiRegister(data: { username: string, email: string, password: string, code: string }) {
  return axios.post('http://localhost:8080/user/register', data)
}

export function apiSendCode(email: string ) {
  const url = `http://localhost:8080/user/send-code?email=${email}`
  return axios.post(url)
}

export function apiGetUserInfo() : any {
  return request.get('/user/info')
}

export function apiAddTraveler(traveler: { name: string, idCard: string, phone: string }) {
  return request.post('/user/add-traveler', traveler)
}

export function apiUpdateUserInfo(userInfo: { username: string, phone: string, email: string, avatar?: string, idCard?: string }) {
  return request.post('/user/update-info', userInfo)
}

export function apiUploadAvatar(formData: FormData) {
  return request.post('/user/upload-avatar', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

export function apiGetTravelers() {
  return request.get('/user/get-travelers')
}

export function apiGetHotelList(page = 1, pageSize = 8): Promise<Result<{ data: Hotel[]; msg: number }>> {
  return request.get('/hotel/getHotelsList', { params: { page, pageSize } })
}

/**
 * 搜索酒店
 * @param params 搜索参数
 * @returns 搜索结果
 */
export function apiSearchHotels(params: {
  destination?: string;
  checkIn?: string;
  checkOut?: string;
  guests?: number;
  page?: number;
  pageSize?: number;
}): Promise<Result<Hotel[]>> {
  return request.get('/hotel/search', { params })
}

// 订单相关api
export function apiCreateBooking(bookingData: {
  hotelId: string | number;
  roomId: number;
  checkIn: string;
  checkOut: string;
  guestCount: number;
  travelers: { name: string; idCard: string; phone?: string }[];
  totalPrice: number;
}) {
  return request.post('/booking/create', bookingData)
}


// 获取用户订单
export function apiGetUserBookings() {
  return request.get('/booking/user-bookings')
}

export function apiCancelBooking(bookingId: string | number) {
  return request.post(`/booking/cancel/${bookingId}`)
}

export function apiGetBookingDetail(bookingId: string | number) {
  return request.get(`/booking/${bookingId}`)
}

// 评论相关api
export function apiAddReview(reviewData: {
  hotelId: string | number;
  bookingId: number;
  rating: number;
  content: string;
  images?: string[];
}) {
  return request.post('/review/add', reviewData)
}

export function apiGetHotelReviews(hotelId: string | number, page = 1, pageSize = 10) {
  return request.get('/review/hotel-reviews', {
    params: { hotelId, page, pageSize }
  })
}

/**
 * 获取酒店评论统计信息
 * @param hotelId 酒店ID
 * @returns 评论统计信息，包括平均分和评分分布
 */
export function apiGetHotelReviewStats(hotelId: string | number) {
  return request.get('/review/hotel-stats', {
    params: { hotelId }
  })
}

// 评论图片上传
export function apiUploadReviewImage(formData: FormData): Promise<{code: number, msg?: string, data?: string}> {
  return request.post('/review/upload-image', formData, {
    headers: {
      'Content-Type': 'multipart/form-data'
    }
  })
}

// 酒店位置api
export function apiGetHotelLocation(hotelId: string | number) {
  return request.get(`/hotel/location/${hotelId}`)
}

// 获取酒店详情
export function apiGetHotelDetail(id: string | number) {
  return request.get(`/hotel/detail/${id}`)
} 

// 创建支付订单
export const apiCreatePayment = (bookingId: string | number) => {
  return request.get(`/payment/create/${bookingId}`)
}

// 消息通知相关API
/**
 * 获取用户消息列表
 * @param page 页码
 * @param pageSize 每页大小
 * @returns 消息列表
 */
export function apiGetUserMessages(page = 1, pageSize = 10) {
  return request.get('/message/list', {
    params: { page, pageSize }
  })
}

/**
 * 标记消息为已读
 * @param messageId 消息ID
 * @returns 操作结果
 */
export function apiMarkMessageAsRead(messageId: number) {
  return request.post(`/message/read/${messageId}`)
}

/**
 * 删除消息
 * @param messageId 消息ID
 * @returns 操作结果
 */
export function apiDeleteMessage(messageId: number) {
  return request.delete(`/message/${messageId}`)
}

/**
 * 获取未读消息数量
 * @returns 未读消息数量
 */
export function apiGetUnreadMessageCount() {
  return request.get('/message/unread-count')
}

// 编辑旅客信息
export function apiUpdateTraveler(traveler: { id: string, name: string, idCard: string, phone: string }) {
  return request.post('/user/update-traveler', traveler)
}

// 删除旅客信息
export function apiDeleteTraveler(travelerId: string) {
  return request.delete(`/user/delete-traveler/${travelerId}`)
}



